package com.ruoyi.system.service.impl;

import java.util.*;

import com.ruoyi.common.config.KeChuangConfig;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.Awards;
import com.ruoyi.system.domain.NumData;
import com.ruoyi.system.domain.Project;
import com.ruoyi.system.domain.SysUserRole;
import com.ruoyi.system.mapper.AwardsMapper;
import com.ruoyi.system.mapper.DemandMapper;
import com.ruoyi.system.mapper.SysUserRoleMapper;
import com.ruoyi.system.service.IDemandService;
import com.ruoyi.system.service.ISysRoleService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;


/**
 * 需求Service业务层处理
 *
 * @author EdwardXRX
 * @date 2024-04-12
 */
@Service
public class DemandServiceImpl implements IDemandService {
    @Autowired
    private DemandMapper demandMapper;
    @Autowired
    private AwardsMapper awardsMapper;
    @Resource
    private KeChuangConfig keChuangConfig;
    //    @Resource
//    private ISys sysRoleUserService;
    @Resource
    SysUserRoleMapper sysUserRoleMapper;

    @Resource
    ISysUserService sysUserService;

    /**
     * 查询需求
     *
     * @param id 需求主键
     * @return 需求
     */
    @Override
    public Project selectProjectById(Long id) {
        Project project = demandMapper.selectProjectById(id);
        System.out.println(project);
        System.out.println(project.getClickNum());
        project.setClickNum(project.getClickNum() + 1);
        demandMapper.updateProject(project);
        return project;
    }

    /**
     * 查询需求列表
     *
     * @param project 需求
     * @return 需求
     */
    @Override
    public List<Project> selectProjectList(Project project) {
        return demandMapper.selectProjectList(project);
    }

    /**
     * 新增需求
     *
     * @param project 需求
     * @return 结果
     */
    @Override
    public int insertProject(Project project) {
        //设置为type为需求收集
        project.setType(1);
        //查找审核员
        Long innovationAuditRoleId = keChuangConfig.getInnovationAuditRoleId();
        System.out.println(innovationAuditRoleId);
        List<SysUserRole> sysUserRoles = sysUserRoleMapper.selectUserIdByRoleId(innovationAuditRoleId);
        List<Long> userIds = new ArrayList<>();
        for (SysUserRole sysUserRole : sysUserRoles) {
            SysUser sysUser = sysUserService.selectUserById(sysUserRole.getUserId());
            if (sysUser != null) {
                userIds.add(sysUser.getUserId());
            }
        }
        System.out.println(userIds);
        if (userIds.size() > 2) {
            throw new RuntimeException("审核员不能超过两个,请联系管理员");
        } else if (userIds.size() < 2) {
            throw new RuntimeException("审核员不能少于两个,请联系管理员");
        }

        //设置审核员为两个管理员
        project.setChecker1(userIds.get(0));
        project.setChecker2(userIds.get(1));
        project.setClickNum(0);
        project.setCreateTime(new Date());
        //为不可见
        project.setIsShow(0);
        //待审核的需求
        project.setState(0);
        return demandMapper.insertProject(project);
    }

    /**
     * 修改需求
     *
     * @param project 需求
     * @return 结果
     */
    @Override
    public int updateProject(Project project) {
        return demandMapper.updateProject(project);
    }

    /**
     * 批量删除需求
     *
     * @param ids 需要删除的需求主键
     * @return 结果
     */
    @Override
    public int deleteProjectByIds(Long[] ids) {
        return demandMapper.deleteProjectByIds(ids);
    }

    /**
     * 删除需求信息
     *
     * @param id 需求主键
     * @return 结果
     */
    @Override
    public int deleteProjectById(Long id) {
        return demandMapper.deleteProjectById(id);
    }

    @Override
    public NumData getCount() {
        Integer viewCount = demandMapper.getCountOfInterview();
        NumData numData = new NumData();
        List<Project> needCount = demandMapper.getCountOfNeed();
        List<Project> topicCount = demandMapper.getCountOfTopic();
        List<Project> achievement = demandMapper.getCountOfAchievement();
        List<Awards> awards = awardsMapper.getCountOfAwards();
        if (needCount == null) {
            numData.setNeedCount(0);
            numData.setDemandList(null);
        } else {
            numData.setDemandList(needCount);
            numData.setNeedCount(needCount.size());
        }
        if (topicCount == null) {
            numData.setTopicCount(0);
            numData.setTopicList(null);
        } else {
            numData.setTopicList(topicCount);
            numData.setTopicCount(topicCount.size());
        }
        if (achievement == null) {
            numData.setAchievementCount(0);
            numData.setAchievementList(null);
        } else {
            numData.setAchievementList(achievement);
            numData.setAchievementCount(achievement.size());
        }
        if (awards == null) {
            numData.setAwardsCount(0);
            numData.setAwardsList(null);
        } else {
            numData.setAwardsList(awards);
            numData.setAwardsCount(awards.size());
        }
        if (viewCount == null) {
            numData.setViewCount(0);
        } else {
            numData.setViewCount(viewCount);
        }
        return numData;
    }

    @Override
    public List<Project> selectProjectListByUserId(Long userId) {
        return demandMapper.selectProjectListByUserId(userId);
    }

    @Override
    public List<Project> listDemandByClickNum() {
        return demandMapper.listDemandByClickNum();
    }

    @Override
    public Project getPreProject(Long id, String from, Long userId) {
        //from =1 : 首页 （审核通过//热门
        if (from.equals("1")) {
            System.out.println("11111111111");
            return demandMapper.getPreProject(id);
        } else {
            //个人管理页
            //超级管理员 看到所有项目的所有状态
            if (userId == 1) {
                return demandMapper.getAllPreProject(id);
            } else {
                Map<String, Long> map = new HashMap<String, Long>();
                map.put("id", id);
                map.put("userId", userId);
                //个人  看到所有状态但是自己创建的
                return demandMapper.getPreProjectOfMine(map);
            }
        }
    }

    @Override
    public Project getNextProject(Long id, String from, Long userId) {
        //从首页进入（审核通过的，state=2）
        if (from.equals("1")) {
            return demandMapper.getNextProject(id);
        } else {
            if (userId == 1) {//能看到项目所有状态
                return demandMapper.getAllNextProject(id);
            } else {
                Map<String, Long> map = new HashMap<String, Long>();
                map.put("id", id);
                map.put("userId", userId);

                //个人  看到所有状态但是自己创建的
                return demandMapper.getNextProjectOfMine(map);
            }
        }
    }


}
